<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php include("menu.php"); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" type="text/css" href="datepicker/css/smoothness/jquery-ui-1.7.2.custom.css">
<script type="text/javascript" src="datepicker/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="datepicker/js/jquery-ui-1.7.2.custom.min.js"></script>

<script type="text/javascript" src="jquery-autocomplete/jquery.autocomplete.js"></script>
<script type="text/javascript" src="jquery-autocomplete/lib/jquery.bgiframe.min.js"></script>
<script type="text/javascript" src="jquery-autocomplete/lib/jquery.ajaxQueue.js"></script>
<script type="text/javascript" src="jquery-autocomplete/lib/thickbox-compressed.js"></script>
<link rel="stylesheet" href="jquery-autocomplete/jquery.autocomplete.css"/>

<script type="text/javascript">
	function CheckList(){
        if(document.formaddlist.list.value == "" )
        {
            alert('กรุณาระบุรายการด้วยค่ะ');
            document.formaddlist.list.focus();
            return false;
        }
        if(document.formaddlist.qty.value == "" )
        {
            alert('กรุณาระบุจำนวนด้วยค่ะ');
            document.formaddlist.qty.focus();
            return false;
        }
        if(document.formaddlist.price.value == "" )
        {
            alert('กรุณาระบุราคาต่อหน่วยด้วยค่ะ');
            document.formaddlist.price.focus();
            return false;
        }
        if(document.formaddlist.total.value == "" )
        {
            alert('กรุณาระบุจำนวนเงินด้วยค่ะ');
            document.formaddlist.total.focus();
            return false;
        }
        document.formaddlist.submit();
	}

	function setFocus(){
		document.formaddlist.list.focus();
	}

	function totalcash(){
		var elm1 = document.getElementById("qty").value;
		var elm2 = document.getElementById("price").value;
		var result;
		var total;

		if(document.formaddlist.price.value!="" && document.formaddlist.qty.value!=""){
		result = elm2*elm1;
		
		document.getElementById("total").value = result;
		}
	}
	
	function pricecash(){
		var elm1 = document.getElementById("qty").value;
		var elm2 = document.getElementById("total").value;
		var result;
		var total;

		if(document.formaddlist.total.value!="" && document.formaddlist.qty.value!=""){
		result = Math.round((elm2/elm1)*100)/100;
		
		document.getElementById("price").value = result;
		}
	}
</script>
<?php
$Query = "SELECT product_name FROM product ";
$objQuery = mysql_query($Query) or die("Error Query [" . $Query . "]");
	while($row = mysql_fetch_assoc($objQuery)){
		$line1[]=array($row["product_name"]);
	}
?> 
<script type="text/javascript">
$(document).ready(function(){

	var str_month = <?php echo json_encode($line1).';'; ?>

	$("#list").autocomplete( str_month , {
		mustMatch: false,
		autoFill: false,
		matchContains:true,
		});

});
</script>
</head>
<body onload="setFocus()">
<?php

if($_GET['action']=='addlist'){
	
	if($_POST['list']!="" && $_POST['qty']!="" && $_POST['price']!="" && $_POST['total']!=""){
	$iQuery = mysql_query("SELECT product_id FROM product WHERE product_name = '".$_POST["list"]."'");
	$iRow = mysql_fetch_array($iQuery);
	
	//ยังแก้ไขไม่เสร็จ
	if($iRow){
		mysql_query("UPDATE buy_detail SET product_id='".$iRow['product_id']."', qty='".$_POST["qty"]."', price='".$_POST["price"]."',total='".$_POST["total"]."' WHERE buy_detail_id = '".$_POST['buy_detail_id']."' ");
		//$location = "buy_history_edit.php?id=".$_POST['buy_his_id'];
		header('Location: buy_history_edit.php?id='.$_POST['buy_his_id']);
	} else {
		$save = "ไม่มีชื่อสินค้านี้ กรุณาเพิ่มชื่อสินค้านี้ก่อนค่ะ";
	}
	
	} else {
		$save = "กรุณากรอกข้อมูลให้ครบค่ะ";
	}
}

$objQuery = mysql_query("SELECT * FROM buy_detail,product WHERE buy_detail.product_id = product.product_id AND buy_detail_id = '".$_GET['id']."' ");
$row = mysql_fetch_array($objQuery);
?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>แก้ไขรายการสินค้า</h1>
    <div class="buttons"><a onclick="CheckList();" class="button"><span>บันทึก</span></a><a onclick="JavaScript:window.back();" class="button"><span>กลับ</span></a></div></div>
  <div class="content" style="padding-top: 70px;">

	
      <form action="?action=addlist" name="formaddlist" method="post" id="formaddlist">
      <table class="listshow">
      <thead>
      	<tr>
      		<td class="center">รายการ</td>
      		<td class="center">จำนวน</td>
      		<td class="center">ราคาต่อหน่วย</td>
      		<td class="center">จำนวนเงิน</td>
      	</tr>
      	</thead>
      	<tbody>
      	 <tr>
      		<td class="center"><input type="text" name="list" id="list" size="30" class="listform" value="<?php echo $row['product_name']; ?>" /><input type="hidden" name="buy_detail_id" value="<?php echo $_GET['id']; ?>" /><input type="hidden" name="buy_his_id" value="<?php echo $_GET['hid']; ?>" /></td>
      		<td class="center"><input type="text" name="qty" id="qty" size="10" class="listform" onkeyup="totalcash();" onkeydown="if(event.keyCode==13){document.formaddlist.submit();}" value="<?php echo $row['qty']; ?>" /></td>
      		<td class="center"><input type="text" name="price" id="price" size="10" class="listform" onkeyup="totalcash();" onkeydown="if(event.keyCode==13){document.formaddlist.submit();}" value="<?php echo $row['price']; ?>" /></td>
      		<td class="center"><input type="text" name="total" id="total" size="10" class="listform" onkeyup="pricecash();" onkeydown="if(event.keyCode==13){document.formaddlist.submit();}" value="<?php echo $row['total']; ?>" /></td>
      	</tr>
      	</tbody>
      </table>
      </form>
	<?php echo "<br><font color=\"red\"><center>$save</center></font>"; ?>
  </div>
</div>
</div>
</body>
</html>
